package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.BaseManageService;
import com.atguigu.gmall.product.service.BaseTradeMarkService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class AdminSpuInfoController {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private BaseTradeMarkService baseTradeMarkService;

    @Autowired
    private BaseManageService baseManageService;

    /**
     * spu分页查询
     * @param pageNum
     * @param pageSize
     * @param category3Id
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<IPage<SpuInfo>> getPage(
            @PathVariable("pageNum") Long pageNum,
            @PathVariable("pageSize") Long pageSize,
            @RequestParam("category3Id") Long category3Id) {

        IPage<SpuInfo> spuInfoIPage = spuInfoService.getPage(pageNum, pageSize, category3Id);
        return Result.ok(spuInfoIPage);
    }

    /**
     * spu页面查询所有品牌
     * @return
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> getList() {
        List<BaseTrademark> list = baseTradeMarkService.list(null);
        return Result.ok(list);
    }

    /**
     * 获取销售属性字典
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> getAllBaseSaleAttr() {
        List<BaseSaleAttr> baseSaleAttrList = baseManageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    /**
     * 保存spu信息
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result<Void> saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = spuInfoService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImageList = spuInfoService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }
}
